package com.huishu.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huishu.commons.pojo.Result;
import com.huishu.commons.utlis.BeanUtils;
import com.huishu.user.mapper.MenuMapper;
import com.huishu.user.mapper.RoleMapper;
import com.huishu.user.pojo.dto.PageListDTO;
import com.huishu.user.pojo.dto.RoleDTO;
import com.huishu.user.pojo.model.RoleDO;
import com.huishu.user.pojo.model.RoleMenuDO;
import com.huishu.user.pojo.vo.RoleVO;
import com.huishu.user.service.RoleMenuService;
import com.huishu.user.service.RoleService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator@ljj
 * @since 2023-04-21
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleDO> implements RoleService {

    @Autowired
    private RoleMenuService roleMenuService;

    @Transactional
    @Override
    public Result add(RoleDTO roleDTO) {
        RoleDO role = BeanUtils.copyProperties(roleDTO, RoleDO.class);
        int insert = this.baseMapper.insert(role);
        if (insert > 0) {
            if (CollectionUtils.isNotEmpty(roleDTO.getMenuIds())) {
                List<RoleMenuDO> collect = roleDTO.getMenuIds().stream().map(id -> {
                    RoleMenuDO roleMenuDO = new RoleMenuDO();
                    roleMenuDO.setRoleId(role.getId());
                    roleMenuDO.setMenuId(id);
                    return roleMenuDO;
                }).collect(Collectors.toList());
                roleMenuService.saveBatch(collect);
            }
            return Result.success();
        }
        return Result.fail("添加失败");
    }

    @Transactional
    @Override
    public Result modify(RoleDTO roleDTO) {
        RoleDO role = BeanUtils.copyProperties(roleDTO, RoleDO.class);
        int insert = this.baseMapper.updateById(role);
        if (insert > 0) {
            LambdaQueryWrapper<RoleMenuDO> queryWrapper = Wrappers.lambdaQuery(RoleMenuDO.class)
                    .eq(RoleMenuDO::getRoleId,roleDTO.getId());
            roleMenuService.remove(queryWrapper);
            List<RoleMenuDO> collect = roleDTO.getMenuIds().stream().map(id -> {
                RoleMenuDO roleMenuDO = new RoleMenuDO();
                roleMenuDO.setRoleId(role.getId());
                roleMenuDO.setMenuId(id);
                return roleMenuDO;
            }).collect(Collectors.toList());
            roleMenuService.saveBatch(collect);
            return Result.success();
        }
        return Result.fail("修改失败");
    }

    @Override
    public Result pageList(PageListDTO pageListDTO) {
        Page<RoleDO> page = new Page<>(pageListDTO.getCurrPage(), pageListDTO.getPageSize());
        LambdaQueryWrapper<RoleDO> queryWrapper = Wrappers.lambdaQuery(RoleDO.class)
                .eq(StringUtils.isNotEmpty(pageListDTO.getSearch()),RoleDO::getName,pageListDTO.getSearch());
        return Result.success(this.baseMapper.selectPage(page, queryWrapper));
    }

    @Override
    public Result getRoleInfo(Long id) {
        RoleDO roleDO = this.getById(id);
        RoleVO roleVO = BeanUtils.copyProperties(roleDO, RoleVO.class);
        List<RoleMenuDO> roleMenuList = roleMenuService.list(new QueryWrapper<RoleMenuDO>().lambda().and(obj -> obj.eq(RoleMenuDO::getRoleId, id)));
        if(CollectionUtils.isNotEmpty(roleMenuList)){
            roleVO.setMenuIds(roleMenuList.stream().map(RoleMenuDO::getMenuId).collect(Collectors.toList()));
        }
        return Result.success(roleVO);
    }


    @Transactional
    @Override
    public Result deleteRole(Long id) {
        this.removeById(id);
        // 删除角色菜单关系
        LambdaQueryWrapper<RoleMenuDO> queryWrapper = Wrappers.lambdaQuery(RoleMenuDO.class)
                .eq(RoleMenuDO::getRoleId,id);
        roleMenuService.remove(queryWrapper);
        return Result.success();
    }
}
